//绑定事件初始化
var layer;
var form;
var slider;
var element;
var table;
var colorpicker;
var upload;
var clearTAG = false;
$(document).ready(
    function() {
        //$.ajaxSetup({ timeout: 1500 });
        //show3DLayers();
        layui.use(['form', 'layer', 'element', 'slider', 'table', 'colorpicker', 'upload'], function() {
            table = layui.table;
            layer = layui.layer;
            form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
            slider = layui.slider;
            element = layui.element;
            upload = layui.upload;
            colorpicker = layui.colorpicker;
            form.render('select');
            form.render('checkbox');
            form.render('radio');
            form.render();
            createFlyHtml(); //飞行界面样式
            CropHtml(); //裁剪
            colorpicker.render({
                elem: '#pickColor',
                color:'#D33038',
                done: function(color) {
                    if (color) {
                        contourUniforms.color = Cesium.Color.fromCssColorString(color);
                    }
                }
            });
        });
        //右下角工具列
        createRightBottomToolContainer();
        //工具栏
        createToolMenuContainer();
        // initAppleToolbar();
        //添加工具条要素
        //toolbarsHTML();
        //测量
        // measureHTML();
        //卷帘
        rollerShuttersHTML();
        //分屏
        splitScreenHTML();
        //显示设置
        InitDisplaySettingWin();
        //数据加载
        InitLoadDataWin();
        //书签
        InitUserEntityWin();
        //entity内容
        InitEntityModal();
        //定位
        InitLocationContent();
        //淹没分析
        floodHtml();
        //通视分析
        IntervisibilityHtml();
        //兴趣点查询
        createInterestPointQueryToolbar();
        //空间查询
        createSpatialQueryToolbar();
        //属性查询
        createAttributeQueryToolbar();
        //拉框查询
        createRectangleToolbar();
        //单体化查询
        createDynamicMonomerToolbar();
        //单体化点击模型时生成的图片的div
        showDynamicMonomerDiv();
        //查询结果显示
        createQueryTableDiv();
        //可视域分析
        VisualfieldHtml();
        //地形开挖
        TerrainexcavationHtml();
        //地形夸张
        TerrainExaggerateHtml();
        //位置拾取
        locationPickHTML();
        //剖面
        profileHTML();
        //坡度破面
        InitSlopeGradientHTML();
        //天际线分析
        SkylineHtml();
        //颜色设置
        colorsettingHtml();
        //日照分析
        sunshineHtml();
        //视频投放参数设置
        // videoSettingHTML();
        // VideoDeliveryHTML();
        ///OLMap
        createOlMapContent();
        //WFS
        // InitEditWFSContent();
        //绘制
        InitDrawContent();
        //等高线分析
        ContourContent();
        //填挖方分析
        FillandCutContent() //几何绘制
        Draw3DContent();
        //雨雪雾
        rainSnowFogHtml();
        //淹没分析
        floodlaond();
        //应用分析结果展示
        createAnalysisResultsContent();

        // //人口分布
        // $('#population-map').click(function() {
        //     jsonPopulation();
        // });
        // //GDP
        // $('#gdp-map').click(function() {
        //     gdpPerCapita2008();
        // });
        // //三维数据展示
        // $('#newYorkData').click(function() {
        //     EventModel.NewYork();
        // });
        // $('#photography').click(function() {
        //     OtherData.TilesQXTest();
        // });
        // $('#pointcloud').click(function() {
        //     OtherData.TilesPointCloudTest();
        // });
        // //轨迹czml
        // $('#satellite-track').click(function() {
        //     EventModel.Satellite();
        // });

        // $('#satelliteFacilities-track').click(function() {
        //     OtherData.ClusterPoints();
        // });
        // $('#route-track').click(function() {
        //     addVehicleCZML();
        // });
        // $('#czml-track').click(function() {
        //     addCZMLPath();
        // });

        //右下角工具
        $('#legend').click(function() {
            showLegend();
        });
        $('#fullScreen').click(function() {
            showFullScreen();
        });
        $('#basemapSwitch').click(function() {
            showBasemapSwitch();
        });
        $('#myMap').click(function() {
            showMyMap();
        });
        //功能管理
        $('#loadData').click(function() {
            changeContainerVisible('loadDataWin');
        });
        $('#bookmarks').click(function() {
            changeContainerVisible('userEntityWin');
            if (!userPlotList) {
                EntityM.getEntitysDataByName(Login.UserName());
            }
        });
        $('#export').click(function() {
            var canvas = viewer.scene.canvas;
            var imageWidth = 800;
            var img = Canvas2Image.convertToImage(canvas, canvas.width, canvas.height, 'png');
            var loadImg = document.createElement('a')
            loadImg.href = img.src
            loadImg.download = 'earth'
            loadImg.click()
        });
        //显示设置
        $('#displaySettingA').click(function() {
            EventModel.displaySetting();
            if (!viewModel) colorSettingOnload();
        });
        $("#attributeQueryButton").click(function() {
            attributeQueryButtonHandle();
        });
        //正北
        $('#NorthView').click(function() {
            viewer.camera.setView({
                orientation: {
                    heading: 0.0,
                    pitch: -Cesium.Math.PI_OVER_TWO,
                    roll: 0.0
                }
            });
        });
        // 全屏
        $('#fullExtentMap').click(function() {
            Cesium.Fullscreen.requestFullscreen(document.body)
        });
        // cesium 地图模式
        // Cesium.SceneMode.COLUMBUS_VIEW //哥伦布视图  
        // Cesium.SceneMode.SCENE2D //2维模式
        // Cesium.SceneMode.SCENE3D //3维模式
        // Cesium.SceneMode.MORPHING //模式之间变换，例如二维到三维 
        // 二维
        $('#changeMap2D').click(function() {
            if (viewer.scene.mode != Cesium.SceneMode.SCENE2D) {
                viewer.scene.morphTo2D(1);
            }
        });
        // 三维
        $('#changeMap3D').click(function() {
            if (viewer.scene.mode != Cesium.SceneMode.SCENE3D) {
                viewer.scene.morphTo3D(1);
            }
        });
        //清除
        $('#RemoveAllDSBtn').click(function() {
            RemoveAllLayers();
        });
        // 地图全屏 
        //$('#app').on('click', '.cesium-fullscreenButton', function (e) {
        //    if (($(e.currentTarget).attr('title')).indexOf('Exit') > -1) {
        //        $(e.currentTarget).parents('.vue-grid-item').removeClass('mapFullscreen')
        //    } else {
        //        $(e.currentTarget).parents('.vue-grid-item').addClass('mapFullscreen')
        //    }
        //})
        //// vr全屏
        //$('#app').on('click', '.cesium-vrButton', function (e) {
        //    if (($(e.currentTarget).attr('title')).indexOf('Exit') == -1) {
        //        $(e.currentTarget).parents('.vue-grid-item').removeClass('mapFullscreen')
        //    } else {
        //        $(e.currentTarget).parents('.vue-grid-item').addClass('mapFullscreen')
        //    }
        //})
        ////esc退出
        //$(document).keydown(function (event) {
        //    if (event.keyCode == 27) {
        //        $('.mapFullscreen').removeClass('mapFullscreen')
        //    }
        //});



        //搜索设置
        var serch_btn = window.setTimeout(function() {
            $('.cesium-geocoder-searchButton').off();
            $('.cesium-geocoder-searchButton').click(function() {
                EventModel.search();
            });
            window.clearTimeout(serch_btn);
        }, 6000)
        $("#draw").click(function() {
            changeContainerVisible('drawContent');
            var visible = $("#drawContent").is(":visible");
            if (visible) {
                DrawFigure();
            }
            if (!tracker) {
                initDrawHelper();
            }
        });
        $("#draw3D").click(function() {
            changeContainerVisible('draw3DContent');
            if (!handlerPoint_ellipse) {
                InitDraw3D();
            }
        });
        //查询
        //兴趣点查询
        $("#interestPointQuery").click(function(){
            RemoveAllLayers();
            changeContainerVisible('intersetPointQueryDiv');
        });
        //空间查询
        $("#spaceQuery").click(function() {
            query("spatial");
        });
        //属性查询
        $("#attributeQuery").click(function() {
            query("attribute");
        });
        //拉框查询
        $("#rectangleQuery").click(function() {
            query("rectangle");
        });
        //画圆查询
        $("#circleQuery").click(function() {
            query("circle");
        });
        //模型单体化
        $("#dynamicMonomer").click(function() {
            //MapStatic.flyTo({ lon: 120.047650, lat: 43.874439, height: 772 });  //科尔沁
            query("dynamicMonomer");
        });
        //sql查询
        $("#sqlQuery").click(function() {
            query("sql");
        });
        //点选
        $("#pickQuery").click(function() {
            identifyQuery(1);
        });
        //穿透
        $("#identify").click(function() {
            identifyQuery(2);
        });
        //框选
        $("#boxQuery").click(function() {
            rectangleIdentifyQuery();
        });
        //分析
        //剖面
        $("#profile").click(function() {
            changeContainerVisible('profileContent');
        });
        //投屏
        //$("#video").click(function () {
        //    // Video();
        //    changeContainerVisible('VideoDeliveryContent');
        //});
        //$("#videoSetting").click(function () {
        //    video_handler ? null : InitVideoSetting()
        //    changeContainerVisible('videoSettingContent');
        //});
        //特效
        //黑夜白天交替
        $("#dayNight").click(function() {
            ShowDayAndNight();
        });
        //雨雪雾
        $("#rainSnowFog").click(function() {
            changeContainerVisible('rainSnowFogContent');
        });
        //颜色设置
        $("#colorsetting").click(function() {
            changeContainerVisible('colorsettingContent');
        })
        $("#flyTool").click(function() {
            changeContainerVisible('flyContent');
        });
        //$("#WFSEditer").click(function () {
        //    changeContainerVisible('editWFSContent');
        //    if (!geoserverUrl) InitEditer()
        //});
        $("#linkage").click(function() {
            changeContainerVisible('olMap');
            changeContainerVisible('olMapForm');
            if (!olMap) {
                InitOlMap()
            }
            if ($("#olMap").is(":visible")) {
                $("#cesiumContainer").width("50%");
            } else {
                $("#cesiumContainer").width("100%");
            }
        });
        $("#locationPick").click(function() {
            changeContainerVisible('locationPickContent');
        });
        $("#locationPickHandle").click(function() {
            clearTAG = false;
            LocationPickHandle()
        });
        $("#locationPickClear").click(function() {
            clearTAG = true;
            LocationPickClear()
        });
        $('#measureTool').click(function() {
            if (!handlerDis) {
                measureInit()
            }
            changeContainerVisible('measureContent');
        });
        //淹没分析单击
        $('#floodanalysis').click(function() {
            changeContainerVisible('floodContent');
            $("#height_val").html("高度值：");
        });
        //通视分析
        $('#Intervisibility').click(function() {
            if (!Handler_inter) {
                IntervisibilityOnload()
            }
            changeContainerVisible('IntervisibilityContent');
        });
        //可视化分析
        $('#Visualfield').click(function() {
            //if (!Visualfield_handler) InitVisualfield();
            changeContainerVisible('VisualfieldContent');
        });
        $('#Visualfield_chooseView').click(function() {
            addViewerdPoint();
        });
        $('#Visualfield_clear').click(function() {
            clearViewerdPoint();
        });
        $("#videoDeliveryClear").click(function() {
            videoDeliveryClearHandle();
        });
        //地形开挖
        $('#Terrainexcavation').click(function() {
            if (!terrainClipPlan) {
                TerrainexcavationOnload()
            }
            changeContainerVisible('TerrainexcavationContent');
        });
        //地形开挖
        $("#TerrainExaggerate_Excavation").click(function() {
            var value = parseFloat($("#TerrainExaggerateVal").val());
            viewer.scene.terrainExaggeration = value;
            viewer.scene.globe.terrainExaggeration = value;
        })
        //地形夸张
        $('#terrainExaggerate').click(function() {
            changeContainerVisible('terrainExaggerateContent');
        });
        //天际线分析
        $('#Skyline').click(function() {
            if (!skyline_polygonHandler) {
                skylineOnload()
            }
            changeContainerVisible('SkylineContent');
        });
        //裁剪工具
        $('#Crop').click(function() {
            if (!crop_handlerPolygon) {
                CropOnload()
            }
            changeContainerVisible('CropContent');
        });
        //日照分析
        $('#sunshine').click(function() {
            changeContainerVisible('sunshineContent');
        });
        //等高线
        $('#contour').click(function() {
            changeContainerVisible('contourContent');
        });
        //填挖方分析
        $('#fillandcut').click(function() {
            changeContainerVisible('fillandcutContent');
        })
        $('#rollerShutters').click(function() {
            //if (!blackMarble) {
            //    InitRollerShutter()
            //}
            //changeContainerVisible('rollerShuttersContent');
            var homeCameraParam = viewer.camera.positionCartographic;
            var lon = Cesium.Math.toDegrees(homeCameraParam.longitude);
            var lat = Cesium.Math.toDegrees(homeCameraParam.latitude);
            var height = homeCameraParam.height;
            $("#mapSplitIframe").show();
            $("#mapSplitIframe").attr("src", "/MapServer/Portal/RollerShutter.html");
            $("#mapSplitIframe").attr("lontitude", lon);
            $("#mapSplitIframe").attr("latitude", lat);
            $("#mapSplitIframe").attr("height", height);
        });
        $('#splitScreen').click(function() {
            //if (splitType == "layout_style_2") {
            //    splitType = "layout_style_1";
            //    layout(1, 1);
            //} else {
            //    setLayout("layout_style_2");
            //}
            var homeCameraParam = viewer.camera.positionCartographic;
            var lon = Cesium.Math.toDegrees(homeCameraParam.longitude);
            var lat = Cesium.Math.toDegrees(homeCameraParam.latitude);
            var height = homeCameraParam.height;
            $("#mapSplitIframe").show();
            $("#mapSplitIframe").attr("src", "/BasePlatform/demo/splitScreen.html");
            $("#mapSplitIframe").attr("lontitude", lon);
            $("#mapSplitIframe").attr("latitude", lat);
            $("#mapSplitIframe").attr("height", height);
        });
        $('#location').click(function() {
            changeContainerVisible('locationContent');
        });
        $('#slopeGradient').click(function() {
            changeContainerVisible('slopeGradientContent');
        });
        $('#height').click(function() {
            measureHeight(viewer);
        });
        $('#clear').click(function() {
            RemoveAllLayers();
        });
        $("#profileHandle").click(function() {
            profileHandle()
        })
        $("#profileClear").click(function() {
            profileClear()
        })
        $('#transparentHandle').click(function() {
            setTransparentLayer($("#transparentHandle").attr("layerId"), $("#transparentHandle").attr("layerType"));
        });
        $('#flyToLayerHandle').click(function() {
            flyToLayer($("#flyToLayerHandle").attr("layerId"), $("#flyToLayerHandle").attr("layerType"));
        });
        $('#heatmap').click(function() {
            heatMap();
        });
        $('#userLayout').click(function() {
            userLayout();
        });
        $('#userManage').click(function() {
            location.href = '/index.html';
        });
        //地图故事
        $('#mapStory').click(function() {
            changeContainerVisible('mapStoryContent');
        });
        InitSlopeGradientEvent();
        setTimeout(function() {
            $("#mask-load").hide();
            RainOrSnowOrFog();
            SunshineOnload();
            contourOnload();
            FillandCutOnload();
            //窗口事件
            $('.move').mousedown(function() {
                toolbarMove(this);
            });
            $('.close').click(function() {
                $(this.parentNode.parentNode).css('display', 'none');
                //if($("#statisticsFullScreen")[0].style.display !== "none")
                $("#statisticsFullScreenDiv").hide();
                // removeAllEntities();
                RemoveAllLayers();
            });
            $('.closeAnalysisResultWin').click(function() {
                $(this.parentNode.parentNode.parentNode.parentNode).css('display', 'none');
                //if($("#statisticsFullScreen")[0].style.display !== "none")
                $("#statisticsFullScreenDiv").hide();
                // removeAllEntities();
                // RemoveAllLayers();
                featuresDataSource.entities.removeAll();
                var layerguid = m_MapConfigLayers["analyseMapLayerGuid"];
                if(cesium.layer3DList[layerguid]){
                    viewer.scene.imageryLayers.remove(cesium.layer3DList[layerguid]);
                    delete cesium.layer3DList[layerguid];
                }
            });
            $('.toggle_win').click(function() {
                $(".content", this.parentNode.parentNode).slideToggle();
            });
            EventModel.userEntity();
            EventModel.dataLoad();
        }, 3000);
        jQuery.curCSS = function(element, prop, val) {
            return jQuery(element).css(prop, val);
        };
        var time = new Date();
        var day = ("0" + time.getDate()).slice(-2);
        var month = ("0" + (time.getMonth() + 1)).slice(-2);
        var today = time.getFullYear() + "-" + (month) + "-" + (day);
        $('#sunshine_selDate').val(today);
        //window.onunload = function (e) {
        //    videoDeliveryClearHandle();
        //}
        //设置所有操作面板位置
        //$(".functionWin").css("left", $("#cesiumContainer").offset().left + 100);
        //$(".functionWin").css("top", $("#cesiumContainer").offset().top + 250);
        //initInfoWindow();//infowindow
    }
);